package ee.ut.math.tvt.swing;

import java.awt.event.KeyEvent;

import javax.swing.JTextField;

/**
 * IntegerTextField, lubame kasutajal sisestada ainult täisarve. Lubatud sistada
 * 0-9 ning kasutada klahvi backspace
 * 
 * @author Margus Hanni
 * 
 */
@SuppressWarnings("serial")
public class JDoubleField extends JTextField {

	public JDoubleField() {
		super();
	}

	public JDoubleField(Integer integer) {
		super(integer);
	}

	public void processKeyEvent(KeyEvent ev) {
		char c = ev.getKeyChar();
		if (ev.getKeyCode() != KeyEvent.VK_BACK_SPACE && !Character.isDigit(c) && c != 46) {
			ev.consume();
			return;
		}
		super.processKeyEvent(ev);
	}

	public Double getValue() {
		return Double.parseDouble(this.getText());
	}

	public void setValue(double value) {
		setText(Double.toString(value));
	}

}
